<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-08-02
 * Time: 下午 5:33
 * 资源类
 */
namespace app\api\controller;
use think\Controller;
use think\Db;
class Qun extends Controller{
    //会员设置群置顶
    public function setQunIsZd($member_id,$qun_id,$is_zd){
        $res = Db::name('qun_member')->where('member_id',$member_id)->where('qun_id',$qun_id)->update(array('is_zd'=>$is_zd));
        if($res === false){
            return false;
        }else{
            return true;
        }
    }
    //群成员
    public function qunMemberList($qun_id,$page,$count){
        $data = Db::name('qun_member')
            ->join('member','snake_member.member_id = snake_qun_member.member_id')
            ->where('snake_qun_member.qun_id',$qun_id)
            ->limit($count)
            ->page($page)
            ->select();
        return $data;
    }
    //修改群信息
    public function saveQunInfo($qun_id,$name,$notice,$img){
        $data = array(
            'name'=>$name,
            'notice'=>$notice,
            'img'=>$img
        );
        $res = Db::name('qun')->where('qun_id',$qun_id)->update($data);
        if($res === false){
            return false;
        }else{
            return true;
        }
    }
    //修改资源
    public function saveSource($source_id,$data){
        $res = Db::name('source')->where('source_id',$source_id)->update($data);
        if($res === false){
            return false;
        }else{
            return true;
        }
    }
    //群资源列表
    public function qunSourceList($qun_id,$page,$count){
        $data = Db::name('qun_source')
            ->join('source','snake_source.source_id = snake_qun_source.source_id')
            ->where('snake_qun_source.qun_id',$qun_id)
            ->limit($count)
            ->page($page)
            ->select();
        return $data;
    }
    //群标签
    public function addQunSig($qun_id,$arr){
        $res = Db::name('sig')->where('qun_id',$qun_id)->insertAll($arr);
        if($res){
            return true;
        }else{
            return false;
        }
    }
    //群标签列表
    public function qunSigList($qun_id,$page,$count){
        $data = Db::name('sig')->where('qun_id',$qun_id)->limit($count)->page($page)->select();
        if(!empty($data)){
            //查询标签下有多少资源
            for($i=0;$i<count($data);$i++){
                $where['qun_id'] = $qun_id;
                $where['sig_id'] = ['like','%'.$data[$i]['sig_id'].'%'];
                $data[$i]['count'] = Db::name('qun_source')->where($where)->count();
            }
        }
        return $data;
    }
    //设置群资源标签
    public function setQunSourceSig($qun_id,$source_id,$sig_id){
        $res = Db::name('qun_source')->where('qun_id',$qun_id)->where('source_id',$source_id)->update(array('sig_id'=>$sig_id));
        if($res === false){
            return false;
        }else{
            return true;
        }
    }
    //解散群
    public function outQun($qun_id){
        Db::startTrans();
        try{
            //删除所有群成员
            Db::name('qun_member')->where('qun_id',$qun_id)->delete();
            //删除该群
            Db::name('qun')->where('qun_id',$qun_id)->delete();
            // 提交事务
            Db::commit();
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return false;
        }
    }
    //退出群
    public function tuiQun($qun_id,$member_id){
        $res = Db::name('qun_member')->where('qun_id',$qun_id)->where('member_id',$member_id)->delete();
        if($res){
            return true;
        }else{
            return false;
        }
    }
    //转让群
    public function zhuanQun($qun_id,$member_id,$qun_member_id){
        Db::startTrans();
        try{
            //群主变成员
            Db::name('qun_member')->where('qun_id',$qun_id)->where('member_id',$member_id)->update(array('is_master'=>0));
            //群主变成员
            Db::name('qun_member')->where('qun_id',$qun_id)->where('member_id',$qun_member_id)->update(array('is_master'=>1));
            // 提交事务
            Db::commit();
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return false;
        }
    }
    //添加群
    public function addQun($name,$notice,$img,$member_id){
        $data = array(
            'name'=>$name,
            'notice'=>$notice,
            'img'=>$img,
            'time'=>time(),
            'member_id'=>$member_id
        );
        $id = Db::name('qun')->insertGetId($data);
        if($id){
            return $id;
        }else{
            return false;
        }
    }
    //添加群成员
    public function addQunMember($data){
        $res = Db::name('qun_member')->insert($data);
        if($res){
            return true;
        }else{
            return false;
        }
    }
}